package com.boyi.cycling_master.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.boyi.cycling_master.R;
import com.boyi.cycling_master.bean.ResponseBean;
import com.boyi.cycling_master.common.ActivityStackManager;
import com.boyi.cycling_master.common.SnackUtil;
import com.boyi.cycling_master.https.utils.UserRetrofit;
import com.boyi.cycling_master.ui.base.BaseActivity;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author Lee_Mofeel
 * @version 1.0
 * @date ${Date} ${Time}
 * @Description
 */

public class UpdatePasswordActivity extends BaseActivity {
    @BindView(R.id.btn_nav_icon)
    ImageView btnNavIcon;
    @BindView(R.id.edt_register_user_phone)
    MaterialEditText edtRegisterUserPhone;
    @BindView(R.id.edt_register_checked_code)
    MaterialEditText edtRegisterCheckedCode;
    @BindView(R.id.tv_btn_to_next)
    TextView tvBtnToNext;

    private String phoneNum;

    @Override
    public int getContentViewId() {
        return R.layout.activity_update_pwd;
    }

    @Override
    public void initLogic() {

    }

    @Override
    public void getBundleExtras(Bundle bundle) {
        phoneNum = bundle.getString("phoneNum");
    }

    @OnClick({R.id.btn_nav_icon, R.id.tv_btn_to_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_nav_icon:
                finish();
                break;
            case R.id.tv_btn_to_next:
                String password = edtRegisterCheckedCode.getText().toString();
                String confirm = edtRegisterUserPhone.getText().toString();
                if(password.equals(confirm))
                    toUpdate(password);
                else
                    SnackUtil.SnackShort(edtRegisterCheckedCode,"确保两次密码一致");
                break;
        }
    }

    private void toUpdate(String password) {
        Map<String, String> map = new HashMap<>();
        map.put("userPhone", phoneNum);
        map.put("userPassword",password);
        UserRetrofit.getInstance().toRetrievePwd(new Callback<ResponseBean>() {
            @Override
            public void onResponse(Call<ResponseBean> call, Response<ResponseBean> response) {
                ResponseBean bean = response.body();
                if(bean== null)
                    return;
                SnackUtil.SnackShort(edtRegisterCheckedCode,bean.getResultMessage());
                if(bean.getResultCode().equals("200")){
                    ActivityStackManager.getInstance().killActivity(RetrievePwdActivity.class);
                    ActivityStackManager.getInstance().killActivity(UpdatePasswordActivity.class);
                }
            }

            @Override
            public void onFailure(Call<ResponseBean> call, Throwable t) {

            }
        },map);
    }
}
